<?php
//微信授权入口
class CheckuserController extends \BaseController {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index(){
		//
        error_reporting(0);
//        return Redirect::to('customers/certification');

        $pub = Pub::find(1);
        $id = $pub->AppID;
        $secret = $pub->AppSecret;
        $http = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$id&secret=$secret&code=".$_GET['code']."&grant_type=authorization_code";
        $json = Wechat::http_request_json($http);

        $data=json_decode($json,true);
//        var_dump($data);
        $info_http = "https://api.weixin.qq.com/sns/userinfo?access_token=".$data['access_token']."&openid=".$data['openid']."&lang=zh_CN";
        $info_json= Wechat::http_request_json($info_http);//这个地方不能用file_get_contents
        $info_data=json_decode($info_json,true);

        if($info_data['errcode'] != '40003'){//是否查询出用户信息
            $state = Input::get('state');
            $state_arr = explode('_',$state);
            if(is_numeric($state)){
                $count = Work::where('openid', $info_data['openid'])->count();
                if(1 != $count){
                    echo '无效操作';
                }else{

                    $work = Work::where('openid', $info_data['openid'])->firstOrFail();
                    Session::put('customer_id', $work->id);
                }
            }else{

                if(count($state_arr) > 1){
                    if($state_arr[0] == 'rates'){

                        $count = Customer::where('openid', $info_data['openid'])->count();

                        Session::put('user_info', $info_data);
                        if(1 != $count){
                            return Redirect::route('customers.reg', array('state' => $state));
                        }else{

                            $customer = Customer::where('openid', $info_data['openid'])->firstOrFail();
                            Session::put('customer_id', $customer->id);
                        }
                        return Redirect::route('services.show', array('id' => $state_arr[1],'customer_id'=>$customer->id));
                    }elseif($state_arr[0] == 'area'){//通过发送位置进入商城

                        $count = Customer::where('openid', $info_data['openid'])->count();

                        Session::put('user_info', $info_data);
                        if(1 != $count){
                            return Redirect::route('customers.reg', array('state' => $state));
                        }else{

                            $customer = Customer::where('openid', $info_data['openid'])->firstOrFail();
                            Session::put('customer_id', $customer->id);
                            return Redirect::route('machines.index', array('area_id' => $state_arr[1]));
                        }


                    }elseif($state_arr[0] == 'new'){//工人扫二维码，完善客户信息

                        $count = Work::where('openid', $info_data['openid'])->count();
                        if(1 != $count){
                            echo '无效操作';
                        }else{

                            $work = Work::where('openid', $info_data['openid'])->firstOrFail();
                            Session::put('customer_id', $work->id);
                        }


                        return Redirect::route('mounts.prepare', array('code'=>$state_arr[2]));
                    }elseif($state_arr[0] == 'share'){//点击分享链接效果
                        $count = Customer::where('openid', $info_data['openid'])->count();


                        $pub = Pub::find(1);
                        if (Cache::has('token'))
                        {
                            //
                            $value = Cache::get('token');
                        }else{
                            $value =  Wechat::get_access_token($pub->AppID,$pub->AppSecret);
                            Cache::put('token', $value, 240);

                        }

                        $url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$value.'&openid='.$info_data['openid'];

                        $sb = file_get_contents($url);
                        $sbinfo = (array)json_decode($sb);
                        if($sbinfo['subscribe'] == 0){
                            header('Location: http://mp.weixin.qq.com/s?__biz=MzA3MTM4MzgwMw==&mid=202915383&idx=1&sn=304aebcc6c8ee0b99ec75162f0603ea8');
                        }
                        if($count > 0){
                            $notice = '您已经是雷谛的尊贵用户了吧？无法重复接受邀请！';
                            return View::make('errors.error',compact('notice'));
                        }else{



                            Session::put('user_info', $info_data);
                            return Redirect::route('customers.reg', array('type' => 'invite','customer_id'=>$state_arr[1]));
                        }
                    }else{
                        $count = Work::where('openid', $info_data['openid'])->count();
                        if(1 != $count){
                            echo '无效操作';
                        }else{

                            $work = Work::where('openid', $info_data['openid'])->firstOrFail();
                            Session::put('customer_id', $work->id);
                        }
                        if($state_arr[0] == 'service'){
                            return Redirect::route('services.show', array('id' => $state_arr[1]));
                        }elseif($state_arr[0] == 'paidan'){
                            return Redirect::route('services.paidan', array('id' => $state_arr[1]));
                        }else{
                            return Redirect::route('services.edit', array('id' => $state_arr[0],'code'=>$state_arr[1]));
                        }
                    }

                }

                $count = Customer::where('openid', $info_data['openid'])->count();

                Session::put('user_info', $info_data);
                if(1 != $count){
                    return Redirect::route('customers.reg', array('state' => $state));
                }else{

                    $customer = Customer::where('openid', $info_data['openid'])->firstOrFail();
                    Session::put('customer_id', $customer->id);
                }
            }
//            $materials = Material::all();
//        $a = App::make('dimensions'); // 用法
//        echo $a->get_access_token('wxcaa2bcd4a1bd473a','2eed6952191ff11f5760d688465ce3ca');
//            var_dump($info_data);
//            Session::put('userinfo', $info_data);
//            $count = Customer::where('openid', $info_data['openid'])->count();

            switch ($state)
            {
                case 'center':
                    return Redirect::action('CustomersController@index');
                case is_numeric($state):
                    return Redirect::route('services.edit', array('id' => $state));
                case 'service':
                    return Redirect::action('ServicesController@index');
                    break;
                case 'request':
                    return Redirect::action('MountsController@first');
                    break;
                case 'mount':
                    return Redirect::action('MountsController@index');
                    break;
                case 'material':
                    return Redirect::action('MaterialsController@index');
                    break;
                case 'order':
                    return Redirect::action('ServicesController@orders');
                    break;
                case 'jifen':
                    return Redirect::action('PointsController@index');
                    break;
                case 'commodity':
                    return Redirect::action('CommoditiesController@index');
                    break;
                case 'fujin':
                    return Redirect::route('mounts.fujin');
                    break;
                case 'supplement':
                    return Redirect::route('customers.edit',array('id'=>$customer->id));
                    break;
                case 'data':
                    return Redirect::route('customers.center');
                    break;
                default:
                    echo "No number between 1 and 3";
            }



//            if($userinfo){
//                if($_GET['state'] == 'dzp'){
//                    $run_url = 'http://job.vbafang.com/zp/index.php/Index/rote?uid='.$userinfo['id'];
//                }else{
//                    $run_url = '/dy/'.$_GET['state'];
//                }
//                redirect($run_url, 'refresh');
//            }else{
//                $run_url = '/dy/gift?rote='.$_GET['state'];
//                redirect($run_url, 'refresh');
//            }

        }

    }



}
